package com.cc.entity.annotation;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/*
 *  <!-- 一、给基本类型(8中)+String -->
    <bean name="redPig" class="com.cc.factory.RedPig">
       <!--  方式一：通过set方法给属性注入值  -->
        <property name="age" value="12"/>
        <property name="name" value="乔治"/>
    </bean>

    @Component 相与定义了一个 id就是类的首字母小写
    <bean id="user" class="com.cc.entity.User">
        @Value
        <property name="" value="">
    </bean>
 */

/* XML中 ：
  <bean id="abc"  class="com.cc.entity.User">
    </bean>
   等价于：
       使用@Component
 * */
//@Component("abc")
@Service("abc")
@Scope("prototype")
public class User {

    // 相当于xml配置 <property name="name" value="王大锤">
    @Value("王小锤")
    private String name;
    private int age;

    /* 属性是引用类型 */
    // 相当于xml配置的 <property name="dog" ref="dg1"></property>
    // @Autowired //可以放到属性上 也可以在属性set方法上
    // @Qualifier("dg2")
    // private Dog dog;

    @Resource // 默认按字段名注入 ,如果找不到对应name的bean，则按类型注入
    private Dog dog;

    // 生命周期初始化方法
    @PostConstruct
    public void setup() {
        System.out.println("开始对User类对象，进行业务初始化了.......");
    }

    // 生命周期销毁方法
    @PreDestroy 
    public void clearup() {
        System.out.println("开始对User类对象，进行销毁前的操作了.......");
    }

    // 数组
    private String[] arr;

    // List集合
    private List<String> list;

    private List<Dog> dogs; // ?

    // Map集合
    private Map<String, String> map;

    private Map<String, Dog> dogMap;// ?

    public User() {
    }

    // 相当于xml配置<constructor-arg type="Dog" ref="dg1"/>
//    @Autowired(required=false)
    public User(Dog dog) {
        this.dog = dog;
    }

//    
//    @Autowired(required=false)
    public User(int ag, Dog dg) {
        this.dog = dg;
        this.age = ag;
    }

    public User(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + ", dog=" + dog + "]";
    }

    public String show() {
        return "User [name=" + name + ", age=" + age + ",\n dog=" + dog + "，\narr=" + arr + ",\nlist=" + list
                + ",\ndogs=" + dogs + ",\nmap=" + map + ",\ndogMap=" + dogMap + "]";
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String[] getArr() {
        return arr;
    }

    public void setArr(String[] arr) {
        this.arr = arr;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public List<Dog> getDogs() {
        return dogs;
    }

    public void setDogs(List<Dog> dogs) {
        this.dogs = dogs;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Map<String, Dog> getDogMap() {
        return dogMap;
    }

    public void setDogMap(Map<String, Dog> dogMap) {
        this.dogMap = dogMap;
    }

}
